package Thread;


import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class MyCallable implements Callable {
    @Override
    public Object call() throws Exception {
        int sum = 0;
        //线程内部需要实现的操作
        for (int i=0;i<10;i++){
            System.out.println("线程："+Thread.currentThread()+i);
            sum+=i;
        }
        return sum;//自动装箱，转为Integer
    }
}

class CallableTest{
    public static void main(String[] args) {
        MyCallable myCallable = new MyCallable();
        FutureTask futureTask1 = new FutureTask(myCallable);
        FutureTask futureTask2 = new FutureTask(myCallable);
        Thread t1 = new Thread(futureTask1);
        Thread t2 = new Thread(futureTask2);
        t1.start();
        t2.start();

        try {
            Object sum1 = futureTask1.get();
            Object sum2 = futureTask2.get();
            System.out.println(Thread.currentThread().getName()+":结果："+sum1);
            System.out.println(Thread.currentThread().getName()+":结果："+sum2);
        }catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
